package cn.itsource.crm.service.impl;

import cn.itsource.crm.domain.Contract;
import cn.itsource.crm.domain.ContractPaymentDetails;
import cn.itsource.crm.mapper.ContractMapper;
import cn.itsource.crm.service.IContractPaymentDetailsService;
import cn.itsource.crm.service.IContractService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.OddNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Service
public class ContractServiceImpl extends BaseServiceImpl<Contract> implements IContractService {

    //合同明细
    @Autowired
    private IContractPaymentDetailsService contractPaymentDetailsService;
    //定金订单
    /*@Autowired
    private DepositOrderServiceImpl depositOrderService;*/
    //注入合同mapper
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 保存合同时自动生成合同单号
     * @param contract
     */
    @Override
    public void save(Contract contract) {
        //通过单号生成工具类获取单号
        String oddNum = OddNumUtil.getoddNum();
        //设置到合同对象中
        contract.setSn(oddNum);
        //保存到数据库
        super.save(contract);

        //1.如果是通过定金订单生成的合同，则需要将定金订单和合同的id都保存到中间表中，2.将定金保存到合同明细中
        if (contract.getDepositOrder() != null){
            Long contractId = contract.getId();
            //将定金也保存到合同明细中
            Long depositOrderId = contract.getDepositOrder().getId();
            //将中间表的id都放到map中，以便通过sql保存
            Map<String,Long> map = new HashMap<>();
            map.put("contractId",contractId);
            map.put("depositOrderId", depositOrderId);
            //1.将定金订单id和合同id都保存到中间表中
            saveToDepositContract(map);

            //2.将定金金额等信息，保存到合同明细中
            //创建合同明细对象
            ContractPaymentDetails detail = new ContractPaymentDetails();
            //设置所属合同
            detail.setContract(contract);
            //设置金额---定金金额
            detail.setMoney(contract.getDepositOrder().getSum());
            //设置付款时间
            detail.setPayTime(contract.getDepositOrder().getSignTime());
            //设置是否付款
            detail.setIsPayment(true);
            //保存
            contractPaymentDetailsService.save(detail);


        }
    }

    /**
     * 保存 定金订单id和合同id 到中间表
     * @param map
     */
    private void saveToDepositContract(Map map) {
        contractMapper.saveToDepositContract(map);
    }

    /**
     * 通过合同id，删除中间表的数据
     * @param id
     */
    private void deleteDepositContract(Serializable id) {
        contractMapper.deleteDepositContract(id);
    }

    /**
     * 级联删除，先删除合同明细表的数据，删除中间表关联数据，再删除合同表数据
     * @param id
     */
    @Override
    public void delete(Serializable id) {
        //删除合同明细
        contractPaymentDetailsService.deleteByContractId(id);
        //删除中间表的数据
        deleteDepositContract(id);

        //删除合同表数据
        super.delete(id);
    }



    @Override
    public AjaxResult findByDepositId(Long id) {

        //查询中间表，判断合同是否已经存在
        Long contractId = contractMapper.findByDepositId(id);
        if (contractId != null){
            return new AjaxResult();
        }
        return new AjaxResult(false);
    }
}
